<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">

<title>施工单位 详细</title>
<link rel="stylesheet" href="css/vivo.css">
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap.min.css">
<link rel="stylesheet" href="js/bootstrap2/css/bootstrap-table.css">
<link rel="stylesheet" href="css/common.css">
<link href="js/select2/select2.min.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="js/webuploader-0.1.5/webuploader.css" />

<script src="js/jquery-1.11.1.js"></script>
<script src="js/bootstrap2/js/bootstrap.min.js"></script>

<script src="js/bootstrap2/js/bootstrap-table.js"></script>
<script src="js/bootstrap2/js/bootstrap-table-zh-CN.js"></script>
<script src="js/select2/select2.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/My97DatePicker/WdatePicker.js"></script> 
<script type="text/javascript" src="js/webuploader-0.1.5/webuploader.js"></script>
<script type="text/javascript" src="js/layui-v2.1.5/layui/layui.all.js"></script> 
<script type="text/javascript" src="js/sharefunction.js"></script> 
<script type="text/javascript" src="js/form.js"></script> 
<!-- 图片放大 -->
<script src="js/postbird-img-glass/postbird-img-glass.js" ></script>
<style type="text/css">
	/* 上传按钮与文本框对其 */
	div[id^='filePicker']{
		vertical-align:middle;
	}
</style>
</head>

<body>
<!-- 核准材料 -->
<div>
	<!-- 表单开始 -->
	<form action="customer/dm/addProofread" id="proofreadForm"
		method="post">
		<input type="hidden" id="proofread_articleId">
		<div class="panel panel-default">
			<div class="panel-body ">
				<table cellpadding="10" cellspacing="5" class="mytable">
					<tr>
						<td width="90"><span class="style_btn">施工单位</span></td>
						<td>
							<!-- 由于调用的是同一个controller，所以在施工人员页面上初始化的 -->
							<select id="proofreads2" style="width: 200px;" >
							</select>
						</td>
					</tr>
					<tr>
						<td width="90"><span class="style_btn">装修管理协议</span></td>
						<td>
							<!-- 缩略图容器 -->
							<div id='list_zxgl'></div> <!-- 上传按钮 -->
							<div id='filePicker_zxgl' style="display: none">上传</div>
						</td>
					</tr>
					<tr>
						<td><span class="style_btn">安全协议</span></td>
						<td>
							<!-- 缩略图容器 -->
							<div id='list_aqxy'></div> <!-- 上传按钮 -->
							<div id='filePicker_aqxy' style="display: none">上传</div>
						</td>
					</tr>
					<tr>
						<td><span class="style_btn">动火作业许可证</span></td>
						<td>
							<!-- 缩略图容器 -->
							<div id='list_dhzy'></div> <!-- 上传按钮 -->
							<div id='filePicker_dhzy' style="display: none">上传</div>
						</td>
					</tr>
					<tr>
						<td><span class="style_btn">临时电源许可证</span></td>
						<td>
							<!-- 缩略图容器 -->
							<div id='list_lsdy'></div> <!-- 上传按钮 -->
							<div id='filePicker_lsdy' style="display: none">上传</div>
						</td>
					</tr>
					<tr>
						<td><span class="style_btn">开工许可证</span></td>
						<td>
							<!-- 缩略图容器 -->
							<div id='list_kgxk'></div> <!-- 上传按钮 -->
							<div id='filePicker_kgxk' style="display: none">上传</div>
						</td>
					</tr>
				</table>
				<!-- 修改时添加变更说明 -->
				<div style="margin:10px;display:none;" id="pf_div">
					变更说明<textarea rows="3" name="remark" id="pfRemark" style="width:70%"></textarea><span style="color:red;">*</span>
				</div>
			</div>
		</div>
		<div id="proofreadBtn" style="width:100%;text-align:center; padding-top:20px">
			<br/>
			<br/>
		</div>
		<input name="proofreadId" id="entityId_id" value="${id }" hidden="hidden"/>
	</form>
	<!-- 按钮模版 -->
	<div style="display: none;">
		<!-- <button class="btn btn-danger radius" id="proofreadEdit" onclick="proofreadOpenEdit(this);" type="button" >启用编辑</button> -->
		<button class="btn btn-danger radius" id="proofreadTijiao" onclick="proofreadSubmit(this);" type="button" >提交</button>
		<button class="btn btn-danger radius" id="proofreadSave" style="margin-left: 10px;" onclick="proofreadSubmit(this);" type="button" >保 存</button>
		<a class="btn btn-default radius" id="proofreadReset" style="margin-left: 10px;" href="javascript:;" onclick="resetForm(this);">重置</a> 
	</div>
	<!-- 表单结束 -->
	<script type="text/javascript">
		var proofFuidList = new Array();//删除附件id列表
		var proofreadOpt=0;//0 ：保存 ;1：提交 ;2：修改
		var proofreadEntityId = $("#proofreadForm [name='proofreadId']").val();//实体id
		var fkroomId = parent.fkroomId;
		var fkcustomerId = parent.fkcustomerId;
		var pafkdunitId;
		var proofReadFlag = '${proofReadFlag}';
		$(function() {
			if(proofReadFlag=='2'){
				$("#pf_div").show();
			}
			
			//初始化表单
			mySubmit("proofreadForm", initProofreadData, proofreadSuccess);
			initWebUpload("filePicker_zxgl", "list_zxgl", "装修管理协议", "proofread_articleId", decorationManageUpSccess, undefined, null);
			initWebUpload("filePicker_aqxy", "list_aqxy", "安全协议", "proofread_articleId", decorationManageUpSccess, undefined, null);
			initWebUpload("filePicker_dhzy", "list_dhzy", "动火作业许可证", "proofread_articleId", decorationManageUpSccess, undefined, null);
			initWebUpload("filePicker_lsdy", "list_lsdy", "临时电源许可证", "proofread_articleId", decorationManageUpSccess, undefined, null);
			initWebUpload("filePicker_kgxk", "list_kgxk", "开工许可", "proofread_articleId", decorationManageUpSccess, undefined, null);
			//当刚开始这个div没有被展示 display=none时 这个div的宽高会为0，导致不能点击，所以要手动设置高度
			$("div[id^=filePicker] div").css({"width":"60px","height":"30px"});
			initProofreadFile();
			initS2();
			
		});
		//
		function initProofreadFile(){
			//获取附件数据					
			getFileList(proofreadEntityId,"装修管理协议","#list_zxgl",setProofRFL,imgPlus);
			getFileList(proofreadEntityId,"安全协议","#list_aqxy",setProofRFL,imgPlus);
			getFileList(proofreadEntityId,"动火作业许可证","#list_dhzy",setProofRFL,imgPlus);
			getFileList(proofreadEntityId,"临时电源许可证","#list_lsdy",setProofRFL,imgPlus);
			getFileList(proofreadEntityId,"开工许可","#list_kgxk",setProofRFL,imgPlus);
		}
		//保存成功回调
		function dProofeadDataSuccess(data, status) {
			layer.close(layer.index);
			if (data.msg) {
				var i = layer.alert("操作成功", {
					offset : '20%',
					shade : 0
				}, function() {
					parent.tabKey = 3;
					location.reload();
					layer.close(i);
				});
			}
		}
		function decorationManageUpSccess(file,data){
			successImg(file.id);
			
		}
		
		//本页面图片放大通用方法,同时隐藏上传按钮与图片删除按钮
		function imgPlus(data){
			$.each(data,function(i,v){
				hideFileBtn(v.id);
				PostbirdImgGlass.init({
			    	domSelector:"img",
			    	animation:true,
			    	//height:'80%',
			    	myDocument:window.parent.parent.parent
				});
			});
		}
		//隐藏附件删除按钮
		function hideFileBtn(id){
			//$("#"+id+" span.uploadcancel").hide();
			$(".uploadcancel").hide();
		}
		//缩略图上传成功回调
		function successImg(id){
			PostbirdImgGlass.init({
				//domSelector : "#"+id+" img",
				domSelector : "img",
				animation : true,
				myDocument : window.parent.parent.parent
			});
		}
		//提交表单
		function proofreadSubmit(_this){
			var t=$(_this).html();
			if(proofreadOpt!=2){
				if(t=="提交"){
					proofreadOpt=1;
				}else if(t=="保存"){
					proofreadOpt=0;
				}
				$("#proofreadForm").submit();
			}else{
				var approvalCount = ${approvalCount};
				if(approvalCount == 0 ){
					//提交表单
					$("#proofreadForm").submit();
				}else{
					layer.msg("此条数据已提交过审批，不能重复提交");
				}
			}
			
		}
		//启用编辑
		function proofreadOpenEdit(_this){
			layer.confirm('开启编辑状态？',{
				shade:[0.1, '#fff']
			},function(){
				//删除编辑按钮
				$(_this).remove();
				//添加按钮
				$("#proofreadBtn").prepend($("#proofreadReset"));
				$("#proofreadBtn").prepend($("#proofreadSave"));
				//开启编辑功能
				$("#proofreadForm select").attr("disabled",false);
				$("#proofreadForm input[name='file']").attr("disabled",false);
				//上传按钮启用
				imgProofBtnShow();
				layer.close(layer.index);
			});
		}
		//初始化select2
		function initS2(){
			var $s2=$("#proofreads2");
			//获取该客户下的所有施工单位
			$.getJSON("customer/dm/getAllDecorateUnit",{
				fkcustomerId:fkcustomerId,
				roomId:fkroomId,
			},function(data,status){
				if(data != null){
					var t=$s2.select2();
					$s2.find("*").remove();
					$s2.append("<option value=\"\">请选择施工单位</option>");
					$s2.val("").trigger("change");
					$.each(data,function(i,v){
						$s2.append("<option value="+v.id+">"+v.unitName+"</option>");
					});
					//初始化表单数据,由于需要给select2，所以要在s2加载完成之后初始化表单数据
					initProofreadForm($s2);
				}
			});
			//s2 change事件,修改装修方案全局id变量
			$s2.change(function() {
				pafkdunitId = $s2.val();
			});
		}
		
		//初始化表单数据,如果页面有对应数据则变为编辑状态
		//初始化表单数据，该方法在施工人员 初始化select2插件的最后调用
		function initProofreadForm($s2){
			if(proofreadEntityId == "" || proofreadEntityId==null || proofreadEntityId==undefined){
				//添加按钮
				$("#proofreadBtn").prepend($("#proofreadReset"));
				$("#proofreadBtn").prepend($("#proofreadSave"));
				$("#proofreadBtn").prepend($("#proofreadTijiao"));
				$("#proofreadForm div[id^='filePicker']").css("display","inline-block");
			}else{
				$.getJSON("customer/dm/getCheckDataById",{
					id:proofreadEntityId
				},function(data){
					if(data!=null){
						//查询到数据则开启编辑状态
						//操作字段改为修改
						proofreadOpt=2;
						
						//添加按钮
						//$("#proofreadBtn").prepend($("#proofreadEdit"));
						//对select2设置默认值
						$s2.val(data.fkdunitId).trigger("change");
						if(proofReadFlag == "1"){
							//锁定表单
							$("#proofreadForm select").attr("disabled",true);
							$("#proofreadForm input[name='file']").attr("disabled",true);
							//禁止点击上传按钮
							$("#proofreadForm div[id^='filePicker']").hide();
						}else{
							//添加按钮
							$("#proofreadBtn").prepend($("#proofreadReset"));
							$("#proofreadBtn").prepend($("#proofreadSave"));
							$("#proofreadForm .uploadcancel").show();
							$("#proofreadForm div[id^='filePicker']").css("display","inline-block");
						}
					}
				});
			}
		}
		//设置待删除附件的值到集合中
	function setProofRFL(id,_this){
		layer.confirm('删除该图片？',{
			title:'警告',
			icon:0,
			shade:[0.1, '#fff']
		},function(){
			proofFuidList.push(id);
			//删除指定缩略图
			$("#"+id).remove();
			layer.close(layer.index);
		});
	}

	//显示图片删除按钮
	function imgProofBtnShow(){
		//显示图片删除按钮 
		$("div.file-item span").show();
		//禁止点击上传按钮
		$("#proofreadForm div[id^='filePicker']").unbind("click").show();
	}
	
		function initProofreadData() {
			var mli=layer.load(2);
			var remark = $("#pfRemark").val();
			if(proofreadOpt == "2"){
				if(remark == "" || remark == undefined){
					layer.msg("变更说明不能为空");
					layer.close(mli);
					return null;
				}
			}
			if($("#proofreads2").val()==""){
				layer.close(mli);
				layer.alert("请选择施工单位",{
					offset:'20%',
					shade:0.1
				});
				return null;
			}else{
				return {
					opt:proofreadOpt,
					remark : remark,
					id:proofreadEntityId,
					fkcustomerId : fkcustomerId,
					fkroomId : fkroomId,
					fkdunitId : pafkdunitId,
					accessoryCaList : "[" + $("#proofread_articleId").val() + "]",
					fIds:proofFuidList
				};
			}
		}
		
		function proofreadSuccess() {
			layer.close(layer.index);
			layer.alert("操作成功!",{
				offset:'20%',
				shade:0
			},function(){
				parent.location.reload();
				parent.parent.tabKey=3;
				parent.parent.tabToken=0;
			})
		}
	</script>
</div>
</body>
</html>